import Rte from '..'
import { RteCommandCallback, RteCommandCallbackArg, RteCommandProp } from './declate'

const COMMAND_CALLBACK: Map<string, RteCommandCallback> = new Map()

export default class RteCommand {
  static register<T extends RteCommandProp | string>(command: T, callback: RteCommandCallback) {
    COMMAND_CALLBACK.set(command, callback)
  }

  public constructor(public editor: Rte) {}

  public run<T extends RteCommandProp | string>(command: T, value?: RteCommandCallbackArg) {
    const fn = COMMAND_CALLBACK.get(command)
    if (typeof fn === 'function') {
      fn.call(this.editor, value)
    }
  }
}
